import { ref } from "vue";
import { jobGet } from "../api/job";
import { JobType } from "../types/job";
export const useJobList = () => {
  const jobList = ref<Array<JobType>>([]);
  const loading = ref(false);
  const finished = ref(false);
  let page = 0;
  const onLoad = () => {
    console.log("load执行了"); //onLoad执行的时候，会主动将loading改为true
    jobGet(page).then((res) => {
      let { results } = res.data;
      if (results.length) {
        jobList.value = [
          ...jobList.value, //原数据
          ...results, //新数据
        ];
        loading.value = false; //方便下次继续触底
        page++;
      } else {
        finished.value = true; //结束触底分页的行为
      }
    });
  };
  return {
    loading,
    finished,
    onLoad,
    jobList,
  };
};
